//对接相关设置,订单相关设置在以下几个渠道不展示，添加到项目设置里

var curHost = window.location.host;

if(curHost.indexOf('www.fsyuncai') !=-1){
    var orderSetIdArr = ['GJP0000001','D000000033']; //管家婆和无人仓
}else{
    var orderSetIdArr = ['GJP0000001','UW00000001'];
}



channel()

function channel() {

    if($("#customerId").val().length<10 && $("#customerId").val()!=""){
        $.Huimodalalert('客户ID最少要输入10个字符！',2000);
        return false;
    }
    $.ajax({

        url:root.dockingplatform.listChannelConfig,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":10,
            "customerId":$("#customerId").val(),
            "customerName":$("#customerName").val(),
        }),
        success: function (data) {
            console.log(data)
            couponlist(data.data.result);
            $('.M-box3').pagination({
                pageCount: data.data.totalPage,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    $.ajax({
                        /* root.member.adminMember2CList*/
                        url: root.dockingplatform.listChannelConfig,
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":10,
                            "customerId":$("#customerId").val(),
                            "customerName":$("#customerName").val(),

                        }),
                        success: function (result) {
                            couponlist(result.data.result);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}
var idsss=""

var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function couponlist(data, index){
    console.log(data)
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
        $.each(data, function (i, field) {
            var rowData = new Array();
            rowData.push(field.customerId);//0
            rowData.push(field.customerName);//1
            rowData.push(field.customerBelong);//2
            rowData.push(field.customerPlatform);//3
            rowData.push(field.interfaceType);//4
            rowData.push(field.customerStatus);//5
            rowData.push(field.id);//6
            rowData.push(field.productSetted);//7
            rowData.push(field.customerCode);//8
            rowData.push(field.description);//9
            rowData.push(field.isMatchingItems);//10

            dataSource.push(rowData);

        });

    initDataTable(dataSource);

    function initDataTable(dataSource) {

        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == 1) {
                            val = '我司标准，推送'
                        }else if(val == 2){
                            val = '对方标准，拉取'
                        }else if(val == 3){
                            val = '我司标准，拉取'
                        }
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[5]
                        if (val == 10) {
                            val = '启用'
                        }else {
                            val = '禁用'
                        }
                        return val;
                    },
                    "targets": 5
                },

                {
                    "render": function (data, type, row, meta) {
                        var val = '<a onclick="channelDetails(\'编辑详情\',\'channelDetails.html\',\''+row[0]+'\',\''+row[1]+'\',\''+row[2]+'\',\''+row[3]+'\',\''+row[4]+'\',\''+row[5]+'\',\''+row[9]+'\',\''+row[10]+'\')">编辑详情</a><br><a onclick="projectSetting(\'项目设置\',\'projectSetting.html\',\''+row[8]+'\',\''+row[1]+'\')">项目设置</a><br><a onclick="passwordSetting(\'密钥设置\',\'passwordSetting.html\',\''+row[8]+'\')">密钥设置</a><br><a onclick="InterfaceSetting(\'接口设置\',\'interfaceSetting.html\',\''+row[0]+'\',\''+row[1]+'\',\''+row[4]+'\',\''+row[6]+'\')">接口设置</a>'
                        return val;
                    },
                    "targets": 6
                },

                {
                    "render": function (data, type, row, meta) {
                        if(row[5]==10){  //启用
                            if(row[7]==20){  //商品已设置
                                if(row[4]==2 || row[4]==3){  // 我方标准拉取 对方标准拉取 <a style="opacity: 0.5;">同步商品</a>
                                    var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                    return val;
                                }
                                var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a id="ee" onclick="tongbu(\''+row[0]+'\',this)">同步商品</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                return val;
                            }else {
                                if(row[4]==2 || row[4]==3){  //我方标准拉取 对方标准拉取
                                    var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                    return val;
                                }
                                    var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a id="ee" onclick="tongbu(\''+row[0]+'\',this)">同步商品</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                    return val;
                            }
                        }else {  //禁用状态同步商品按钮不可点击
                            if(row[7]==20){  //商品已设置
                                var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a style="opacity: 0.5;">同步商品</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                return val;
                            }else {  // 商品未设置
                                var val = '<a onclick="channelDetails2(\'商品设置\',\'../ChannelTranslationManage/coupon.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">商品设置</a><br><a style="opacity: 0.5;">价格设置</a><br><a onclick="chongzhi(\''+row[0]+'\',\''+row[6]+'\')">重置商品及价格设置</a><br><a style="opacity: 0.5;" id="tongbu">同步商品</a><br><a onclick="changeSku(\'商品设置\',\'../ChannelTranslationManage/skuPool.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">变更商品池</a>'
                                return val;
                            }
                        }



                    },
                    "targets": 7
                },
                {
                    "render": function (data, type, row, meta) {
                        if(orderSetIdArr.indexOf(row[0]) != -1){
                            return '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a>'
                        }else{
                            return '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                        }
                        // TODO dingjx 下面代码需要重新分析是否需要
                        if(row[5]==10){
                            if(row[7]==20){
                                if(row[4]==2){
                                    var val = '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                    return val;
                                }
                                var val = '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                return val;
                            }else {
                                if(row[4]==2){
                                    var val = '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                    return val;
                                }
                                var val =  '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                return val;
                            }
                        }else {  //禁用状态同步商品按钮不可点击
                            if(row[7]==20){
                                var val = '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                return val;
                            }else {
                                var val = '<a onclick="channelDetails2(\'发货仓设置\',\'houseRanges.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">发货仓设置</a><br><a onclick="channelDetails2(\'订单相关设置\',\'xdgl_ddxgsz.html\',\''+row[0]+'\',\''+row[6]+'\',\''+row[1]+'\')">订单相关设置</a>'
                                return val;
                            }
                        }
                    },
                    "targets": 8
                },

            ]
        });
    };
}
function projectSetting(title,url,customerCode,customerName) {
    sessionStorage.setItem("customerCode",customerCode);
    sessionStorage.setItem("customerName",customerName);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

function passwordSetting(title,url,customerCode) {
    sessionStorage.setItem("customerCode",customerCode);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

function InterfaceSetting(title,url,customerId,customerName,interfaceType,id) {
    sessionStorage.setItem("customerId",customerId);
    sessionStorage.setItem("customerName",customerName);
    sessionStorage.setItem("interfaceType",interfaceType);
    sessionStorage.setItem("id",id);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}

//跳转详情
function channelDetails(title,url,customerId,customerName,customerBelong,customerPlat,platformType,state,description,isMatchingItems){
    //sessionStorage.setItem("id",data);
    var detail={
        "customerId":customerId,
        "customerName":customerName,
        "customerBelong":customerBelong,
        "customerPlat":customerPlat,
        "platformType":platformType, //对接类型
        "state":state,  //状态10：启用
        "description":description,
        isMatchingItems:isMatchingItems,//是都匹配项目
    };
    sessionStorage.setItem("detail",JSON.stringify(detail));

    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//价格设置
function channelDetails2(title,url,data,ids,name){
    sessionStorage.removeItem("click_a")
    sessionStorage.removeItem("click_b")
    sessionStorage.removeItem("zhekoulv")


    sessionStorage.setItem("customerName",name);
    sessionStorage.setItem("id",ids);
    sessionStorage.setItem("customerid",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
//同步商品

function tongbu(cid,e) {
    layer.confirm('确认要同步商品吗？', function(index) {
    var nowTime = new Date().getTime();
    var clickTime = $(this).attr("ctime");
    if( clickTime != 'undefined' && (nowTime - clickTime < 5000)){
        $.Huimodalalert('操作过于频繁，稍后再试！',2000);
        return false;
    }else{
        $(this).attr("ctime",nowTime);
        layer.close(layer.index);
        $.ajax({
            type: "POST",
            url:root.dockingplatform.synData,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                customerId:cid,
            },
            success: function (result) {
                if(result.errorCode=="0"){
                    layer.msg("同步商品成功", {time: 2000});
                        /*$.Huimodalalert('同步商品成功！',2000);*/
              /*      $(pElement).removeAttr("onclick");
                    setTimeout(function () {
                        location.replace(location.href);

                    }, 3000);*/



                }else {
                    layer.msg(result.msg, {time: 2000});
                    /*$.Huimodalalert(result.msg,2000);*/
                    /*$(pElement).attr("style",'opacity: 0.5;');*/
                   channel()
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        });
    }

    });

}
//重置
function chongzhi(zid,ids) {
    layer.confirm('确认要重置商品及价格吗？', function(index) {
    $.ajax({
        type: "POST",
        url:root.dockingplatform.deleteChannelData,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            customerId:zid,
        },
        success: function (result) {
            if(result.errorCode=="0"){
                layer.msg("重置商品及价格设置成功！", {time: 2000});
                /*$.Huimodalalert('重置商品及价格设置成功！',2000);*/
                $.ajax({
                    url:root.dockingplatform.editChannelProductSetted,
                    cache: false,  //禁用缓存
                    type: "POST",
                    dataType:'json',
                    contentType: "application/json",
                    data: JSON.stringify({
                        "id":ids,
                        "productSetted":10
                    }),
                    success: function (data) {
                        channel()
                    },
                    error: function () {
                        layer.msg("重置商品及价格设置成功！", {time: 2000});
                        /*$.Huimodalalert('失败！',2000);*/
                    }
                });


            }else {
                layer.msg(result.msg, {time: 2000});
               /* $.Huimodalalert(result.msg,2000);*/
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest);
        }
       });
    });
}


function addChannel(title,url){
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}



function changeSku(title,url,data,ids,name){
    sessionStorage.setItem("customerName",name);
    sessionStorage.setItem("id",ids);
    sessionStorage.setItem("customerId",data);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}


